const util = require('../../utils/util')

Page({
  data: {
    cellphone: '',
    time: 0,
    interval: null,
    msg: "",
    loading: false
  },
  /**
   * 获取手机号输入框的值
   */
  getCellphone(e) {
    this.setData({
      cellphone: e.detail.value
    })
  },
  /**
   * 验证手机号
   */
  getCode() {
    if (!this.data.cellphone) {
      return this.setData({
        msg: '请输入手机号'
      })
    } else {
      if (!/^1[23456789]\d{9}$/.test(this.data.cellphone)) {
        return this.setData({
          msg: '手机号码有误'
        })
      }
    }
    wx.showLoading({ title: '加载中…', mask: true })
    util.throttle(this.sendCode())
  },
  /**
   * 发送验证码
   */
  sendCode() {
    wx.cloud.callFunction({
      name: 'code',
      data: {
        cellphone: this.data.cellphone
      },
      complete: res => {
        wx.hideLoading()
        const r = res.result || {}
        if (r.code == 200) {
          this.getCodeSuccess(r);
        } else {
          this.setData({
            msg: r.msg
          })
        }
      }
    })
  },
  /**
   * 验证码发送成功的事件
   */
  getCodeSuccess(res) {
    wx.showToast({
      title: res.msg,
      icon: 'success',
      duration: 1000,
      mask: true,
      complete: () => {
        setTimeout(() => {
          this.setData({
            time: 60
          })
          this.data.interval = setInterval(() => {
            if (this.data.time == 0) {
              clearInterval(this.data.interval);
              this.data.interval = null;
            } else {
              this.setData({
                time: this.data.time - 1
              })
            }
          }, 1000);
        }, 500)
        setTimeout(() => {
          this.showCode(res.result)
        }, 5000)
      }
    })
  },
  /**
   * 显示验证码
   */
  showCode(code) {
    wx.showModal({
      title: '验证码',
      content: code,
      showCancel: false
    })
  },
  /**
   * 表单提交事件
   */
  formSubmit(e) {
    const params = e.detail.value || {}
    if (!params.cellphone) {
      return this.setData({
        msg: '手机号不能为空'
      })
    } else {
      if (!/^1[23456789]\d{9}$/.test(params.cellphone)) {
        return this.setData({
          msg: '手机号码有误'
        })
      }
    }
    if (!params.smsCode) {
      return this.setData({
        msg: '验证码不能为空'
      })
    } else {
      if (params.smsCode.length != 4) {
        return this.setData({
          msg: '验证码格式不正确'
        })
      }
    }
    if (!params.referCode) {
      return this.setData({
        msg: '邀请码不能为空'
      })
    }
    if (!params.password) {
      return this.setData({
        msg: '密码不能为空'
      })
    } else {
      if (params.password.length < 6 || params.password.length > 20) {
        return this.setData({
          msg: '密码长度为6-20位'
        })
      }
    }
    this.setData({
      loading: true
    })
    wx.showLoading({ title: '加载中…', mask: true })
    util.throttle(this.register(params))
  },
  /**
   * 注册事件
   */
  register(params) {
    wx.cloud.callFunction({
      name: 'register',
      data: {
        ...params
      },
      complete: res => {
        this.setData({
          loading: false
        })
        wx.hideLoading()
        const r = res.result || {}
        if (r.code == 200) {
          wx.showToast({
            title: r.msg,
            icon: 'success',
            duration: 2000,
            mask: true,
            complete: () => {
              setTimeout(() => {
                wx.navigateTo({
                  url: `../login/login?cellphone=${params.cellphone}`
                })
              }, 500);
            }
          })
        } else {
          this.setData({
            msg: r.msg
          })
        }
      },
    });
  }
})